﻿namespace PMS.Client.FrmUserJobPosition
{
    using AdvancedDataGridView;
    using PMS.BLL;
    using PMS.Client.Properties;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class FrmList : Form
    {
        private JobPositionBLL bll = new JobPositionBLL();
        private ToolStripButton btnAdd;
        private ToolStripButton btnDelete;
        private ToolStripButton btnUpdate;
        private IContainer components = null;
        private TreeGridNode node1 = null;
        private TreeGridNode node2 = null;
        private DataGridViewTextBoxColumn PositionID;
        private TreeGridColumn PositionName;
        private ToolStrip toolStrip1;
        private TreeGridView treeGridView1;

        public FrmList()
        {
            this.InitializeComponent();
            Font font = new Font(this.treeGridView1.DefaultCellStyle.Font, FontStyle.Bold);
            object[] values = new object[2];
            values[0] = "实配领导人";
            this.node1 = this.treeGridView1.Nodes.Add(values);
            this.node1.DefaultCellStyle.BackColor = SystemColors.Highlight;
            this.node1.DefaultCellStyle.ForeColor = Color.White;
            this.node1.DefaultCellStyle.Font = font;
            this.node1.Expand();
            values = new object[2];
            values[0] = "实配非领导人";
            this.node2 = this.treeGridView1.Nodes.Add(values);
            this.node2.DefaultCellStyle.BackColor = SystemColors.Highlight;
            this.node2.DefaultCellStyle.ForeColor = Color.White;
            this.node2.DefaultCellStyle.Font = font;
            this.node2.Expand();
            this.LoadJobPosition();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmAdd add = new FrmAdd();
            add.ShowDialog();
            if (add.DialogResult == DialogResult.OK)
            {
                this.LoadJobPosition();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if ((this.treeGridView1.SelectedRows.Count <= 0) || (this.treeGridView1.SelectedRows[0].Cells[1].Value == null))
            {
                MessageBox.Show("请选择要修改的行！");
            }
            else if (MessageBox.Show("确认删除？", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                int titleID = int.Parse(this.treeGridView1.SelectedRows[0].Cells["PositionID"].Value.ToString());
                if (this.bll.Delete(titleID))
                {
                    this.LoadJobPosition();
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if ((this.treeGridView1.SelectedRows.Count <= 0) || (this.treeGridView1.SelectedRows[0].Cells[1].Value == null))
            {
                MessageBox.Show("请选择要修改的行！");
            }
            else
            {
                FrmUpdate update = new FrmUpdate(int.Parse(this.treeGridView1.SelectedRows[0].Cells["PositionID"].Value.ToString()));
                update.ShowDialog();
                if (update.DialogResult == DialogResult.OK)
                {
                    this.LoadJobPosition();
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmList));
            this.toolStrip1 = new ToolStrip();
            this.btnAdd = new ToolStripButton();
            this.btnUpdate = new ToolStripButton();
            this.btnDelete = new ToolStripButton();
            this.treeGridView1 = new TreeGridView();
            this.PositionName = new TreeGridColumn();
            this.PositionID = new DataGridViewTextBoxColumn();
            this.toolStrip1.SuspendLayout();
            ((ISupportInitialize) this.treeGridView1).BeginInit();
            base.SuspendLayout();
            this.toolStrip1.Items.AddRange(new ToolStripItem[] { this.btnAdd, this.btnUpdate, this.btnDelete });
            this.toolStrip1.Location = new Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.RenderMode = ToolStripRenderMode.System;
            this.toolStrip1.Size = new Size(0x114, 0x36);
            this.toolStrip1.TabIndex = 2;
            this.toolStrip1.Text = "toolStrip1";
            this.btnAdd.Image = Resources.Icons_add;
            this.btnAdd.ImageScaling = ToolStripItemImageScaling.None;
            this.btnAdd.ImageTransparentColor = Color.Magenta;
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new Size(0x24, 0x33);
            this.btnAdd.Text = "增加";
            this.btnAdd.TextImageRelation = TextImageRelation.ImageAboveText;
            this.btnAdd.Click += new EventHandler(this.btnAdd_Click);
            this.btnUpdate.Image = Resources.Icons_update;
            this.btnUpdate.ImageScaling = ToolStripItemImageScaling.None;
            this.btnUpdate.ImageTransparentColor = Color.Magenta;
            this.btnUpdate.Name = "btnUpdate";
            this.btnUpdate.Size = new Size(0x24, 0x33);
            this.btnUpdate.Text = "修改";
            this.btnUpdate.TextImageRelation = TextImageRelation.ImageAboveText;
            this.btnUpdate.Click += new EventHandler(this.btnUpdate_Click);
            this.btnDelete.Image = (Image) manager.GetObject("btnDelete.Image");
            this.btnDelete.ImageScaling = ToolStripItemImageScaling.None;
            this.btnDelete.ImageTransparentColor = Color.Magenta;
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Size = new Size(0x24, 0x33);
            this.btnDelete.Text = "删除";
            this.btnDelete.TextImageRelation = TextImageRelation.ImageAboveText;
            this.btnDelete.Click += new EventHandler(this.btnDelete_Click);
            this.treeGridView1.AllowUserToAddRows = false;
            this.treeGridView1.AllowUserToDeleteRows = false;
            this.treeGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.treeGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            this.treeGridView1.BackgroundColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            this.treeGridView1.Columns.AddRange(new DataGridViewColumn[] { this.PositionName, this.PositionID });
            this.treeGridView1.Dock = DockStyle.Fill;
            this.treeGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
            this.treeGridView1.EnableHeadersVisualStyles = false;
            this.treeGridView1.ImageList = null;
            this.treeGridView1.Location = new Point(0, 0x36);
            this.treeGridView1.Name = "treeGridView1";
            this.treeGridView1.RowHeadersVisible = false;
            this.treeGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.treeGridView1.Size = new Size(0x114, 0x13d);
            this.treeGridView1.TabIndex = 7;
            this.PositionName.DefaultNodeImage = null;
            this.PositionName.HeaderText = "职务名称";
            this.PositionName.Name = "PositionName";
            this.PositionName.Resizable = DataGridViewTriState.True;
            this.PositionName.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.PositionID.HeaderText = "PositionID";
            this.PositionID.Name = "PositionID";
            this.PositionID.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.PositionID.Visible = false;
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x114, 0x173);
            base.Controls.Add(this.treeGridView1);
            base.Controls.Add(this.toolStrip1);
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.Name = "FrmList";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "职务列表";
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            ((ISupportInitialize) this.treeGridView1).EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void LoadJobPosition()
        {
            this.node1.Nodes.Clear();
            this.node2.Nodes.Clear();
            List<JobPosition> list = this.bll.GetList();
            foreach (JobPosition position in list)
            {
                if (position.IsLeader == 1)
                {
                    this.node1.Nodes.Add(new object[] { position.PositionName, position.PositionID });
                }
                else
                {
                    this.node2.Nodes.Add(new object[] { position.PositionName, position.PositionID });
                }
            }
        }
    }
}

